﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Caching;
using System.Timers;

namespace spktNhom4DoAnCuoiKy
{
    public class Global : System.Web.HttpApplication
    {
        AuctionOnlineDataContext db = new AuctionOnlineDataContext();
        
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            Dictionary<string, HttpSessionState> sessionData =
               new Dictionary<string, HttpSessionState>();
            Application["ss"] = sessionData;
            Timer timer1 = new Timer();
            timer1.Interval = 30 * 1000;
            timer1.Enabled = true;
            timer1.Elapsed+=new ElapsedEventHandler(DongPhien);
            timer1.Stop();
            timer1.Start();
            

            
        }
        void DongPhien(object sender, System.Timers.ElapsedEventArgs e)
        {
            //get current sessions
            Dictionary<string, HttpSessionState> sessionData =
   (Dictionary<string, HttpSessionState>)Application["ss"];

          
            var phiens = db.PhienDauGias.Where(x => DateTime.Compare(x.ThoiGianKetThucBan.Value, DateTime.Now) < 0 && x.TrangThaiThang.Value ==0).ToList();
            foreach (var p in phiens)
            {
               //thực hiện đóng phiên
                
                if (p.ChiTietPhienDauGias.Count > 0)
                {
                    ChiTietPhienDauGia ct = p.ChiTietPhienDauGias.OrderByDescending(x => x.ThoiGianRaGia.Value).FirstOrDefault();
                    p.TrangThaiThang = 1;
                    p.ThoiGianThang = ct.ThoiGianRaGia;
                    p.NguoiThang = ct.NguoiRaGia;
                    p.ServerNguoiThang = ct.ServerNguoiRaGia;
                    
                }
                else p.TrangThaiThang = 2;
                db.SubmitChanges();
                
                
                //thông báo
                if (p.ChiTietPhienDauGias.Count > 0)
                {

                    ChiTietPhienDauGia ct = p.ChiTietPhienDauGias.OrderByDescending(x => x.ThoiGianRaGia.Value).FirstOrDefault();
                    string nguoithang = String.Format("{0}@{1}_{2}", ct.NguoiRaGia, ct.Server.VietTatTenQuocGia.ToUpper(), ct.Server.SoThuTuTram);
                    string thongbao = String.Format("Chúc mừng bạn đã thắng sản phẩm {0} với số tiền {1} <br/> Mã Phiên : {2}", p.SanPham.TenSanPham, p.GiaThang, p.MaPhien);
                    foreach (var item in sessionData)
                    {
                        if (item.Value != null)
                        {
                            item.Value.SendMessages(thongbao, nguoithang);
                            item.Value["run"] = true;
                        }
                    }
                    
                }
            }
        }
       
        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            Dictionary<string, HttpSessionState> sessionData =
      (Dictionary<string, HttpSessionState>)Application["ss"];

            if (sessionData.Keys.Contains(HttpContext.Current.Session.SessionID))
            {
                sessionData.Remove(HttpContext.Current.Session.SessionID);
                sessionData.Add(HttpContext.Current.Session.SessionID,
                                HttpContext.Current.Session);
            }
            else
            {
                if (HttpContext.Current != null)
                sessionData.Add(HttpContext.Current.Session.SessionID,
                                HttpContext.Current.Session);
            }
            Application["ss"] = sessionData;

        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.
            Dictionary<string, HttpSessionState> sessionData =
       (Dictionary<string, HttpSessionState>)Application["ss"];
           if (HttpContext.Current !=null)
               
            sessionData.Remove(HttpContext.Current.Session.SessionID);
            Application["ss"] = sessionData;

        }

    }
}
